/* @(#)GSDateDownAction.java    V0.1
 *
 * Project: GSdevelop
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   owen         2006-2-24
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.action;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;

import com.hitrust.gs.form.GSManufactureDownForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSSystemConfigurator;
import com.hitrust.gs.net.GSAuditConstants;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.business.GSFileUploadProcessor;
import com.hitrust.gs.util.date.*;

/**
 * 产品数据下载
 */
public class GSDateDownAction extends GSBaseAction {

    /* (non-Javadoc)
     * @see com.hitrust.gs.service.GSBaseAction#execute(com.hitrust.gs.service.GSBaseForm)
     */
    public String execute(GSBaseForm aForm) throws Exception {
        //FileInputStream fis = null;
        DataInputStream dis = null;
        FileOutputStream fos = null;
        try{
//            URL url = new URL("http://61.152.242.72:7001/v3/back/down/BSWZ_Product_20060309.xls");
            String tDay=DateUtil.getNextUpDay(-1);
            //文件下载
            String Name="/" + GSSystemConfigurator.getParam("gs.purcode") + "_Product_"+tDay+".xls";
            URL url = new URL(GSSystemConfigurator.getParam("gs.down.schema")+"://"+GSSystemConfigurator.getParam("gs.down.host")+":"+GSSystemConfigurator.getParam("gs.down.port")+"/"+GSSystemConfigurator.getParam("gs.down.fullpath")+Name);
            //System.out.print(url);    
            dis = new DataInputStream(url.openStream());
            
           
           
            java.io.File f2 = new File(GSSystemConfigurator.getParam("gs.down.savepath") + System.getProperty("file.separator") + Name);
            
            if(!f2.exists()) f2.createNewFile();
            fos = new FileOutputStream(f2);
            
            byte[] buffer = new byte[512];
            int length = 0;
            while((length = dis.read(buffer,0,512))!=-1){
                fos.write(buffer,0,length);
            }
            
            fos.flush();
            fos.close();
            dis.close();
            aForm.getRequest().setAttribute("filename",Name);
            //aForm.getRequest().setAttribute("f2",f2);
        }
        catch(IOException ioe){
            ioe.printStackTrace();
            aForm.getRequest().setAttribute("action","A401001");
            throw new GSException (GSException.E17792,GSException.E17792_MSG);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        //aForm.getRequest().setAttribute("success",tForm.getSucc());
       // aForm.getRequest().setAttribute("fail",tForm.getFail());
       // aForm.getRequest().setAttribute("faildown",tOutputPath);
        
        return GSBaseConstants.FORWARD_SUCCESS;
  
    }

}
